using MicroMsg.Common.Event;
using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Scene;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace MicroMsg.UI.Page
{
	public class BindEmailPage : BasePage
	{
		private EventWatcher m_syncEmailOK;
		private EventWatcher m_syncEmailErr;
		private EventWatcher m_verifyEmailOK;
		private EventWatcher m_verifyEmailErr;
		private bool isSendedFlag;
		internal Grid LayoutRoot;
		internal panelTitle2Line panelTitle;
		internal PhoneTextBox txtInputEmail;
		internal TextBlock txtDescription;
		internal Button btnSendToVerify;
		private bool _contentLoaded;
		public BindEmailPage()
		{
			this.InitializeComponent();
		}
		private void initData()
		{
			if (AccountMgr.strBindEmail != null && AccountMgr.strBindEmail.get_Length() > 0)
			{
				this.txtInputEmail.set_Text(AccountMgr.strBindEmail);
			}
			if (AccountMgr.EmailVerify)
			{
				this.panelTitle.strTitle = "修改邮箱地址";
				return;
			}
			this.panelTitle.strTitle = "绑定邮箱";
			this.txtDescription.set_Height(0.0);
		}
		private void btnSendToVerify_Click(object sender, RoutedEventArgs e)
		{
			string text = this.txtInputEmail.get_Text().Trim();
			if (text.get_Length() == 0)
			{
				MessageBoxManager.ShowToast("请输入邮箱地址.", 2500, null, false);
				this.txtInputEmail.Focus();
				return;
			}
			if (!BindEmailPage.checkEmail(text))
			{
				MessageBoxManager.ShowToast("邮箱地址格式不正确.", 2500, null, false);
				this.txtInputEmail.Focus();
				return;
			}
			this.showProgressCover("正在绑定邮箱...");
			Log.smethod_5("settings", "start bind email.");
			this.isSendedFlag = false;
			AccountMgr.strBindEmail = text;
			ServiceCenter.sceneNewSync.doScene(1, syncScene.MM_NEWSYNC_SCENE_OTHER);
		}
		protected override void OnPageIn(object[] object_0)
		{
			this.initData();
			this.m_syncEmailOK = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventSyncEmailOK));
			this.m_syncEmailErr = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventSyncEmailErr));
			this.m_verifyEmailOK = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventVerifyEmailOK));
			this.m_verifyEmailErr = new EventWatcher(this, null, new EventHandlerDelegate(this.onEventVerifyEmailErr));
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_VERIFY_EMAIL_SUCCESS, this.m_verifyEmailOK);
			EventCenter.registerEventWatcher(EventConst.ON_NETSCENE_VERIFY_EMAIL_ERR, this.m_verifyEmailErr);
		}
		protected override void OnPageOut()
		{
			if (this.m_syncEmailOK == null)
			{
				return;
			}
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_VERIFY_EMAIL_SUCCESS, this.m_verifyEmailOK);
			this.m_verifyEmailOK = null;
			EventCenter.removeEventWatcher(EventConst.ON_NETSCENE_VERIFY_EMAIL_ERR, this.m_verifyEmailErr);
			this.m_verifyEmailErr = null;
		}
		private void onEventSyncEmailOK(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			ServiceCenter.sceneSendVerifyEmail.doScene(AccountMgr.curUserName);
			Log.smethod_5("settings", "bind email ok.");
		}
		private void onEventSyncEmailErr(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			MessageBoxManager.ShowToast("绑定邮箱失败.", 2500, null, false);
			Log.smethod_5("settings", "bind email error.");
			this.hideProgressCover();
		}
		private void onEventVerifyEmailOK(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			if (!this.isSendedFlag)
			{
				MMMessageBox.Show("发送验证邮箱成功.", true, "确定");
				Log.smethod_5("settings", "send verify email ok.");
				this.hideProgressCover();
				base.GoBack();
			}
		}
		private void onEventVerifyEmailErr(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			MessageBoxManager.ShowToast("发送验证邮箱失败.", 2500, null, false);
			Log.smethod_5("settings", "send verify email error.");
			this.hideProgressCover();
		}
		private static bool checkEmail(string emailAddress)
		{
			return emailAddress != null && emailAddress.get_Length() != 0 && Regex.IsMatch(emailAddress, "^[A-Za-z0-9](([_\\.\\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\\.\\-]?[a-zA-Z0-9]+)*)\\.([A-Za-z]{2,})$", 1);
		}
		private void showProgressCover(string strCoverTitle)
		{
			ProcessingCover.getProgressBar(this.LayoutRoot, strCoverTitle, (SolidColorBrush)Application.get_Current().get_Resources().get_Item("HalfOpacityBrush1"), false, false, null);
		}
		private void hideProgressCover()
		{
			ProcessingCover.releaseProgressBar(this.LayoutRoot);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Setting/BindEmailPage.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.panelTitle = (panelTitle2Line)base.FindName("panelTitle");
			this.txtInputEmail = (PhoneTextBox)base.FindName("txtInputEmail");
			this.txtDescription = (TextBlock)base.FindName("txtDescription");
			this.btnSendToVerify = (Button)base.FindName("btnSendToVerify");
		}
	}
}
